package ATMproject;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;


public class Save extends JPanel implements ActionListener {
    private JButton backBtn;

    private MainPage frame;

    private Icon backButton = Util.getIcon("/atmimage/buttonOff.jpg");
    private Icon saveButton = Util.getIcon("/atmimage/buttonSure.jpg");

    private JTextField saveText = new JTextField();
    private JButton saveBtn = new JButton(saveButton);
    private JLabel savaLabel = new JLabel("请输入您要存入的金额:");

    public Save(MainPage frame) {
        this.frame = frame;

        this.setLayout(null);

        this.setOpaque(false);

//        JLabel titleLabel = new JLabel("存款");
//        titleLabel.setBounds(200, 50, 100, 50);
//        this.add(titleLabel);

        backBtn = new JButton(backButton);
        backBtn.setBounds(650, 150, 100, 50);
        this.add(backBtn);
        backBtn.addActionListener(this);

        //存款框
        saveText.setBounds(300, 150, 200, 25);
        this.add(saveText);

        //确认键
        saveBtn.setBounds(350, 200, 100, 50);
        this.add(saveBtn);
        saveBtn.addActionListener(this);

        //提示
        savaLabel.setBounds(170, 150, 150, 25);
        this.add(savaLabel);


    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (backBtn == e.getSource()) {
            frame.switchPanel("menu");
            return;
        }
        if (saveBtn == e.getSource()) {
            if (Double.valueOf(saveText.getText()) >= 0) {
                ArrayList<User> user = (ArrayList<User>) Util.readObject("D:/4.dat");
                double saveMoney = frame.getUserarraylist().get(frame.getIndex()).getMoney() + Double.valueOf(saveText.getText());
                frame.getUserarraylist().get(frame.getIndex()).setMoney(saveMoney);
                Util.writeObject("D:/4.dat",user);
                saveText.setText("");
                JOptionPane.showMessageDialog(this, "您当前余额为" + saveMoney);
                return;
            } else {
                JOptionPane.showMessageDialog(this, "不能存入负值");
            }
        }
    }
}
